package com.start.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.admin.entity.StartSysMenu;
import com.start.admin.mappers.SysMenuMapper;
import com.start.admin.service.StartSysMenuService;
import com.start.admin.vo.StartSysUserMenuVo;
import com.start.common.entity.StartBaseEntity;
import com.start.common.entity.StartBaseMenu;
import com.start.common.exceptions.StartBusException;
import com.start.common.tools.StartStreamTools;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StartSysMenuServiceImpl extends ServiceImpl<SysMenuMapper, StartSysMenu> implements StartSysMenuService {
    @Override
    public List<StartSysMenu> listByUserId(String userId) {
        List<StartSysUserMenuVo> startSysMenus = getBaseMapper().listByUserId(userId);
        return startSysMenus.stream().filter(StartStreamTools.distinctByKey(StartBaseEntity::getId)).collect(Collectors.toList());
    }

    @Override
    public void updateMenuById(StartSysMenu startSysMenu) {
        if (startSysMenu == null || StringUtils.isEmpty(startSysMenu.getId()))
            throw new StartBusException("被修改对象不能为空");
        if (StringUtils.isEmpty(startSysMenu.getLabel())) throw new StartBusException("名称不能为空");
        if (StringUtils.isEmpty(startSysMenu.getParentId())) {
            updateById(startSysMenu);
            return;
        }
        StartSysMenu parentMenu = getById(startSysMenu.getId());
        if (parentMenu == null) throw new StartBusException("父级菜单不存在");
        startSysMenu.setDirectionType(parentMenu.getDirectionType());
        updateById(startSysMenu);
    }

    @Override
    public void createMenu(StartSysMenu startSysMenu) {
        if (StringUtils.isEmpty(startSysMenu.getLabel())) throw new StartBusException("名称不能为空");
        if (startSysMenu.getType() == 1 && StringUtils.isEmpty(startSysMenu.getCode()))
            throw new StartBusException("请配置按钮权限编码");
        save(startSysMenu);
    }

    @Override
    public StartSysMenu getOneBySystemCode(String systemCode) {
        LambdaQueryWrapper<StartSysMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StartSysMenu::getSystemCode, systemCode);
        return getOne(lambdaQueryWrapper, false);
    }

    @Override
    public boolean userHasPermissions(String userId, String[] permissions) {
        List<String> pams = null;
        if (permissions != null && permissions.length > 0) {
            pams = Arrays.stream(permissions).filter(StringUtils::isNotBlank).collect(Collectors.toList());
        }
        QueryWrapper<StartSysUserMenuVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(pams != null && !pams.isEmpty(), "code", pams);
        Long count = baseMapper.userPermissionCount(userId, queryWrapper);
        if (count == null) {
            count = 0L;
        }
        return  count>0;
    }

    @Override
    public String saveGlobalMenu(StartBaseMenu entity) {
        StartSysMenu menu = JSONObject.parseObject(JSONObject.toJSONString(entity), StartSysMenu.class);
        this.save(menu);
        return menu.getId();
    }

    @Override
    public void saveBatchGlobalMenu(Collection<StartBaseMenu> list) {
        if (list == null || list.isEmpty()) return;
        List<StartSysMenu> menus = JSONArray.parseArray(JSONObject.toJSONString(list), StartSysMenu.class);
        this.saveBatch(menus);
    }

    @Override
    public StartSysMenu getGlobalMenuById(Serializable id) {
        return this.getById(id);
    }
}
